/*
 * 创建日期 2009-11-23
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.common.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *  Cookie工具
 * @author: 王文成
 * @version: 1.0
 * @since 2009-11-23
 */
public class CookieUtil {

    public static final int SECONDS_PER_YEAR = 60 * 60 * 24 * 365;

    private static final Log log = LogFactory.getLog(CookieUtil.class);

    /**
     * 保存Cookie
     * 
     * @param response
     * @param value
     * @param text
     */
    public static void addCookie(HttpServletResponse response, String name, String value) {
        try {
            String v = URLEncoder.encode(value, "utf-8");
            Cookie c = new Cookie(name, v);
            c.setMaxAge(SECONDS_PER_YEAR);
            c.setPath("/");
            response.addCookie(c);
        } catch (UnsupportedEncodingException e) {
            log.error(e);
        }
    }

    /**
     * 删除Cookie
     * 
     * @param response
     * @param name
     */
    public static void removeCookie(HttpServletResponse response, String name) {
        Cookie cookie = new Cookie(name, "");
        cookie.setMaxAge(SECONDS_PER_YEAR);
        cookie.setPath("/");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }

    /**
     * 修改Cookie
     * 
     * @param response
     * @param value
     * @param text
     */
    public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name, String value) {
        Cookie cookie = getCookie(request, name);
        if (cookie == null) {
            cookie = new Cookie(name, value);
            addCookie(response, name, value);
        } else {
            cookie.setValue(value);
        }
    }

    /**
     * 保存Cookie
     * 
     * @param response
     * @param value
     * @param text
     */
    public static void addCookie(HttpServletResponse response, Map<Object, Object> cookieMap) {
        for (Map.Entry entry : cookieMap.entrySet()) {
            String name = String.valueOf(entry.getKey());
            String value = String.valueOf(entry.getValue());
            addCookie(response, name, value);
        }
    }

    /**
     * 取得 Cookie
     * 
     * @param response
     * @param value
     * @param text
     */
    public static String getCookieValue(HttpServletRequest request, String name, String defaultValue) {
        try {
            Cookie cookies[] = request.getCookies();
            if (cookies == null)
                return defaultValue;

            for (Cookie cookie : cookies) {
                if (name.equals(cookie.getName())) {
                    return URLDecoder.decode(cookie.getValue(), "utf-8");
                }
            }
        } catch (UnsupportedEncodingException e) {
            log.error(e);
        }
        return defaultValue;
    }

    /**
     * 取得 Cookie
     * 
     * @param response
     * @param value
     * @param text
     */
    public static Cookie getCookie(HttpServletRequest request, String name) {
        Cookie cookies[] = request.getCookies();
        if (cookies == null)
            cookies = new Cookie[] {};

        for (Cookie cookie : cookies) {
            if (name.equals(cookie.getName())) {
                return cookie;
            }
        }
        return null;
    }

    /**
     * 取得 Cookie
     * 
     * @param response
     * @param value
     * @param text
     */
    public static String getCookieValue(HttpServletRequest request, String name) {
        return getCookieValue(request, name, null);
    }

    /**
     * 取得 CookieMap
     * 
     * @param response
     * @param value
     * @param text
     */
    public static HashMap<String, String> getCookieMap(HttpServletRequest request) {
        Cookie cookies[] = request.getCookies();
        HashMap<String, String> cookieMap = new HashMap<String, String>();
        if (cookies == null)
            return cookieMap;
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            String value = cookie.getValue();
            cookieMap.put(name, value);
        }
        return cookieMap;
    }
}
